//
//  MainTabBarController.swift
//  PersonnalDiary
//
//  Created by 徐宽阔 on 15/9/27.
//  Copyright © 2015年 XDKOO. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.addSubview(plusBtn)
        
        let width = UIScreen.mainScreen().bounds.width / CGFloat(childViewControllers.count)
        
        let rect = CGRect(origin: CGPointZero, size: CGSize(width: width, height: 49))
        
        plusBtn.frame = CGRectOffset(rect, 2 * width, 0);
    }
    
    
    func addChildViewControllers()
    {
        do{
            let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
            let data = NSData(contentsOfFile: path)!
            let dictArray = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
            
            for dict in dictArray as![[String:AnyObject]]
            {
                loadChildViewController(dict["vcName"] as! String, imageName: dict["imageName"] as! String, title: dict["title"] as! String)
            }
        }catch
        {
            loadChildViewController("HomeTableViewController", imageName: "tabbar_home", title: "首页")
            loadChildViewController("MessageTableViewController", imageName: "tabbar_message_center", title: "消息")
            loadChildViewController("NullViewController", imageName: "", title: "")
            loadChildViewController("DiscoverTableViewController", imageName: "tabbar_discover", title: "发现")
            loadChildViewController("ProfileTableViewController", imageName: "tabbar_profile", title: "我")
        }
    }
    
    
    func loadChildViewController(vcName:String,imageName:String,title:String)
    {
        // 动态加载类名
        let namespace = NSBundle.mainBundle().infoDictionary!["CFBundleName"] as! String
        
        let cls: AnyClass = NSClassFromString(namespace + "." + vcName)!
        
        let vc = (cls as! UIViewController.Type).init()
        
        if imageName != ""
        {
        vc.tabBarItem.image = UIImage(named:imageName)
        vc.tabBarItem.selectedImage = UIImage(named:imageName + "_highlighted")
        }
        vc.title = title
        
        let nav = UINavigationController()
        
        nav.addChildViewController(vc)
        
        addChildViewController(nav)
    }
    
    private lazy var plusBtn:UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: Selector("btnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        btn.sizeToFit()
        
        return btn
    }()
    
    @objc private func btnClick()
    {
        print(__FUNCTION__)
    }
}















